package thinker.miracle.business.base;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import thinker.miracle.utils.MathUtil;

/**
 * BaseEntity
 * 
 * @author bo.chen
 *
 */
public class BaseEntity implements Serializable{

	private static final long serialVersionUID = 1L;

	private String createTime;
	
	private String updateTime;
	
	private String version = MathUtil.random();

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}
	
	@Override
	public String toString() {
		return getAllClassFields(this);
	}
	
	private String getAllClassFields(Object object) {
		StringBuilder sb = new StringBuilder("{");
		try{
			if (object != null && object instanceof BaseEntity) {
	            Class<?> clz = object.getClass();  
	            Field[] fields = clz.getDeclaredFields();
	            for (Field field : fields) {
	                if (field.getGenericType().toString().equals("class java.lang.String")) {
	                    Method m = (Method) object.getClass().getMethod("get" + getMethodName(field.getName()));  
	                    String val = (String) m.invoke(object);
	                    if(val != null)
	                    	sb.append(field.getName()).append(":\"").append(val).append("\",");
	                    else
	                    	sb.append(field.getName()).append(":").append(val).append(",");
	                }  
	                if (field.getGenericType().toString().equals("class java.lang.Integer")) {  // 如果类型是Integer  
	                    Method m = (Method) object.getClass().getMethod("get" + getMethodName(field.getName()));  
	                    Integer val = (Integer) m.invoke(object);  
	                    sb.append(field.getName()).append(":").append(val).append(",");
	                }  
	                if (field.getGenericType().toString().equals("class java.lang.Double")) {  // 如果类型是Double 
	                    Method m = (Method) object.getClass().getMethod("get" + getMethodName(field.getName()));  
	                    Double val = (Double) m.invoke(object);  
	                    sb.append(field.getName()).append(":").append(val).append(",");
	                }  
	                if (field.getGenericType().toString().equals("class java.lang.Boolean")) {// 如果类型是Boolean 是封装类  
	                    Method m = (Method) object.getClass().getMethod(field.getName());  
	                    Boolean val = (Boolean) m.invoke(object);  
	                    sb.append(field.getName()).append(":").append(val).append(",");
	                }  
	                if (field.getGenericType().toString().equals("boolean")) {  // 如果类型是boolean 基本数据类型不一样 这里有点说名如果定义名是 isXXX的 那就全都是isXXX的   反射找不到getter的具体名  
	                    Method m = (Method) object.getClass().getMethod(field.getName());  
	                    Boolean val = (Boolean) m.invoke(object);  
	                    sb.append(field.getName()).append(":").append(val).append(",");
	                }  
	                if (field.getGenericType().toString().equals("class java.util.Date")) {  // 如果类型是Date  
	                    Method m = (Method) object.getClass().getMethod("get" + getMethodName(field.getName()));  
	                    Date val = (Date) m.invoke(object);  
	                    if(val != null)
	                    	sb.append(field.getName()).append(":\"").append(val).append("\",");
	                    else
	                    	sb.append(field.getName()).append(":").append(val).append(",");
	                }  
	                if (field.getGenericType().toString().equals("class java.lang.Short")) {  // 如果类型是Short  
	                    Method m = (Method) object.getClass().getMethod("get" + getMethodName(field.getName()));  
	                    Short val = (Short) m.invoke(object);  
	                    sb.append(field.getName()).append(":").append(val).append(",");
	                }  
	            }
	        }
		}catch(Exception e){}
		if(sb != null && sb.length() > 0){
			return sb.delete(sb.length()-1,sb.length()).append("}").toString();
		}else{
			return super.toString();
		}
	}
	
    private static String getMethodName(String fildeName) throws Exception{  
        byte[] items = fildeName.getBytes();  
        items[0] = (byte) ((char) items[0] - 'a' + 'A');  
        return new String(items);  
    }  
}
